package com.zhiqianbao.listener;

import android.text.TextUtils;
import android.util.Log;

import com.baidu.location.Address;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;

import java.util.HashMap;
import java.util.Map;

import cn.trinea.android.common.util.StringUtils;

/**
 * Created by 亮 on 2016/1/9.
 */
public class  MyLocationListener implements BDLocationListener {
    private boolean matf34 = true;
    @Override
    public void onReceiveLocation(BDLocation location) {
        if (location == null) {
            Constants.BD_LOCATION = null;
            return;
        }
        Constants.BD_LOCATION = location;
        //Receive Location
        StringBuffer sb = new StringBuffer(256);
        sb.append("time : ");
        sb.append(location.getTime());
        sb.append("\nerror code : ");
        sb.append(location.getLocType());
        sb.append("\nlatitude : ");
        sb.append(location.getLatitude());
        sb.append("\nlontitude : ");
        sb.append(location.getLongitude());
        sb.append("\nradius : ");
        sb.append(location.getRadius());
        Address address = location.getAddress();
        sb.append("\ncountry : ");
        sb.append(address.country);
        sb.append("\nprovince : ");
        sb.append(address.province);
        sb.append("\ncity : ");
        sb.append(address.city);
        sb.append("\ndistrict : ");
        sb.append(address.district);
        sb.append("\nstreet : ");
        sb.append(address.street);
        sb.append("\nstreetNumber : ");
        sb.append(address.streetNumber);
        sb.append("\naddr : ");
        sb.append(location.getAddrStr());
        //运营商信息
        sb.append("\noperationers : ");
        sb.append(location.getOperators());
        sb.append("\nlocationdescribe : ");
        sb.append(location.getLocationDescribe());// 位置语义化信息
        Log.i("BaiduLocationApiDem", sb.toString());

        //
        if (null == Constants.LOGIN_USER) {
            Constants.LOCATION_CLIENT.unRegisterLocationListener(Constants.LOCATION_LISTENER);
            return;
        }

        final String userId = Constants.LOGIN_USER.getId().toString();
        final String lng = location.getLongitude() + "";
        final String lat = location.getLatitude() + "";
        final String province = location.getProvince() + "";
        final String city = location.getCity() + "";
        final String area = location.getDistrict() + "";
        final String addr = location.getAddrStr();
        if (StringUtils.isBlank(addr)) {
            return;
        }
        if (matf34){
                //调用存储地理位置接口
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Map<String, String> param = new HashMap<>();
                        param.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                        param.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                        param.put("lng", lng);
                        param.put("lat", lat);
                        param.put("province", province);
                        param.put("city", city);
                        param.put("area", area);
                        param.put("detailAddress", addr);
                        String location=HttpUtils.httpPostString(Constants.API_ROOT + "/user/saveUserLocationNew", param);
                        if(!TextUtils.isEmpty(location)) {
                            Log.i("BaiduLocationApiDem1", location);
                        }
                        else{
                            Log.i("BaiduLocationApiDem1", "空");
                        }
                        matf34 = false;
                    }
                }).start();
                Constants.LOCATION_CLIENT.unRegisterLocationListener(Constants.LOCATION_LISTENER);
        }

    }
}
